import {customRef} from 'vue'

export default function <T>(value: T, delay: 200) {
  let timeoutId: number
  return customRef((track, trigger) => {
    return {
      get() {
        track() // 开启追踪：通知vue追踪value的变化
        return value
      },
      set(newValue: T) {
        window.clearTimeout(timeoutId)
        timeoutId = window.setTimeout(() => {
          value = newValue
          trigger() // 触发更新：通知vue重新解析模板
        }, delay)
      }
    }
  })
}